<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-col :span="12">
          <el-form-item label="退料单号" prop="rhmDjbh">
            <el-input
              v-model="queryParams.rhmDjbh"
              placeholder="请输入入库单号"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="rhmWldm">
            <el-input
              v-model="queryParams.rhmWldm"
              placeholder="请输入入库单号"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库仓库" prop="rhmCkdm">
            <el-select
              v-model="queryParams.rhmCkdm"
              clearable
              placeholder="请选择入库仓库"
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!--          <el-form-item label="客户代码" prop="rhmKhdm">-->
          <!--            <el-input-->
          <!--              v-model="queryParams.rhmCkdm"-->
          <!--              placeholder="请输入客户代码"-->
          <!--              clearable-->
          <!--              size="small"-->
          <!--            />-->
          <!--          </el-form-item>-->
          <el-form-item label="客户代码" prop="rhmKhdm">
            <el-select
              v-model="queryParams.rhmKhdm"
              clearable
              filterable
              placeholder="请输入客户代码"
            >
              <el-option
                v-for="item in allCustomerOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="厂商代码" prop="rwmCsdm">
            <el-select
              v-model="queryParams.rwmCsdm"
              clearable
              filterable
              placeholder="请选择厂商代码"
            >
              <el-option
                v-for="item in csOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="入库日期">
            <el-date-picker
              v-model="daterangeRwmJlry"
              size="small"
              :clearable="false"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExcel(1)"
          size="small"
          >明细数据</el-button
        >
      </el-col>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          border
          stripe
          :height="tableH"
          @scrollBottom="load"
          :data="rhdDetList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="退货单号"
            align="center"
            width="135"
            prop="rhdDjbh"
          />
          <el-table-column
            label="客户代码"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhmKhdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allCustomerOptions"
                :value="scope.row.rhmKhdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            align="center"
            prop="rhdWldm"
            width="135"
          />
          <el-table-column label="品名规格" prop="rhdPmgg" width="280" />
          <el-table-column
            label="收货单号"
            :show-overflow-tooltip="true"
            align="center"
            prop="rhmShdh"
            width="180"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="rhdDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.rhdDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="申请数量"
            align="center"
            prop="rhdSqsl"
            width="100"
          />
          <el-table-column
            label="允许数量"
            align="center"
            prop="rhdOksl"
            width="100"
          />
          <el-table-column
            label="拒收数量"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhdNgsl"
          />
          <el-table-column label="入库数量" align="center" prop="rhdRksl" />
          <el-table-column
            label="检验结果"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhdJyjg"
          >
            <template #default="scope">
              <dict-tag :options="jyjgDict" :value="scope.row.rhdJyjg" />
            </template>
          </el-table-column>
          <el-table-column
            label="检验描述"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhdJyms"
          />
          <el-table-column
            label="备注"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhmDesc"
          />
          <el-table-column
            label="开单人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhmJlry"
          />
          <el-table-column
            label="开单日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="rhmJlrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.rwmJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="检验人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhmJyry"
          />
          <el-table-column
            label="检验日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="rhmJyrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.rwmJyrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="入库人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhdRkry"
          />
          <el-table-column
            label="入库日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="rhdRkrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.rwdRkrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="过账人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhmGzry"
          />
          <el-table-column
            label="过账日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="rhmGzrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.rwmGzrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { exportPutStorageAdd, getCs } from "@/api/putStorage/putStorageAdd";
import { getPstorageType } from "@/api/repertory/putstorage";
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { listData, exportRwmReport } from "@/api/putStorage/rwmReport";
import { exportRhdReport, rhmList } from "@/api/putStorage/rhmReport";
import { getAllCustomerTwo } from "@/api/project/customer";

export default {
  name: "RhmReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      serachChildren: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      tableH: document.body.clientHeight - 300,
      single: true,
      activeTab: "first",
      // 非多个禁用
      multiple: true,
      rhdDetList: [],
      rwdDateTotal: [],
      rwdCsTotal: [],
      rwdCkTotal: [],
      rwdWldmList: [],
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      openChildren: false,
      // 总条数
      total: 0,
      total2: 0,

      //入库类型
      psTypeOptions: [],

      //存储厂商
      csOptions: [],

      //有效存储仓库
      storageOptions: [],
      //检验结果字典
      jyjgDict: [
        { dictLabel: "允收", dictValue: "OK", listClass: "default" },
        { dictLabel: "拒收", dictValue: "NG", listClass: "default" },
        { dictLabel: "特采", dictValue: "TC", listClass: "default" },
      ],

      //所以存储仓库
      allStorageOptions: [],

      //有效存储储位
      cWOptions: [],

      //所有存储储位
      allCwOptions: [],

      // 弹出层标题
      title: "",
      addDate: true,
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      daterangeRwmJlry: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      rkmxList: [],
      rktjList: [],
      qgmxList: [],
      rowData: null,
      qgdDetCountList: [],
      serachAddList: [],
      unitFlagOptions: [],
      //所有客户
      allCustomerOptions: [],
      selectioned: [],
      type1: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        rhmDjbh: null,
        rhmCkdm: null,
        rhmKhdm: null,
        rhmWldm: null,
        index: 0,
      },

      // 表单参数
      form: {},
      // 表单校验
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    //入库类型字典
    getPstorageType(0).then((response) => {
      this.psTypeOptions = response.data;
      for (let i of this.psTypeOptions) {
        i.listClass = "default";
      }
    });
    //客户代码
    getAllCustomerTwo().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode;
        i.listClass = "default";
      }

      this.allCustomerOptions = response.data;
    });
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //存放位置
    getstorageType("0").then((response) => {
      this.cWOptions = response.data;

      for (let item of this.cWOptions) {
        item.dictLabel = item.storageDefaultstorage;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });

    getstorageType("0").then((response) => {
      this.storageOptions = response.data;

      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });

    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });

    getAll().then((response) => {
      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });

    getCs().then((response) => {
      this.csOptions = response.data;
      for (let item of this.csOptions) {
        item.dictLabel = item.csmCs;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRwmJlry && "" != this.daterangeRwmJlry) {
        this.queryParams.params["beginRhdRkrq"] = this.moment(
          this.daterangeRwmJlry[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endRhdRkrq"] = this.moment(
          this.daterangeRwmJlry[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      rhmList(this.queryParams).then((response) => {
        //明细
        this.rhdDetList = this.rhdDetList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        this.getList();
      }
      return 0;
    },

    handleSearch() {
      this.openSearch = true;
      this.title = "查询来料进货统计分析";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.openChildren = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },

    clickRwm(row) {},
    //导出Excel
    handleExcel(index) {
      this.queryParams.index = index;
      const queryParams = this.queryParams;
      exportRhdReport(queryParams).then((response) => {
        this.download(response.msg);
        this.queryParams.index = 0;
      });
    },
  },
};
</script>

<style scoped></style>
